package com.icebartech.base.modules.user.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.icebartech.base.modules.user.mapper.AppRolePermissionMapper;
import com.icebartech.base.modules.user.model.AppRolePermission;
import com.icebartech.base.modules.user.service.AppRolePermissionService;
import com.icebartech.core.components.RedisComponent;
import com.icebartech.core.constants.IcebartechConstants;
import com.icebartech.core.modules.AbstractService;
import com.icebartech.core.modules.BaseMapper;

@Service
public class AppRolePermissionServiceImpl extends AbstractService implements AppRolePermissionService {
    
    @Autowired
    private AppRolePermissionMapper appRolePermissionMapper;
    
    @Autowired
    private RedisComponent redisComponent;
    
    @Override
    public BaseMapper getBaseMapper() {
        return this.appRolePermissionMapper;
    }
    
    
    @Override
    public List<AppRolePermission> findAppRolePermissionByRoleKey(String roleKey) {
    	return appRolePermissionMapper.findAppPermissionByRoleKey(roleKey);
    	/*
        List<AppRolePermission> appRolePermissions = getRolePermissionFromRedis(roleKey);
        if (null == appRolePermissions) {
            appRolePermissions = appRolePermissionMapper.findAppPermissionByRoleKey(roleKey);
            putRolePermission2Redis(roleKey, appRolePermissions);
        }
        */
    }
    
    @Override
    public List<Long> findPermissionsByRoleKey(String roleKey) {
    	List<Long> permissionIds = new ArrayList<>();
    	List<AppRolePermission> appRolePermissions = findAppRolePermissionByRoleKey(roleKey);
    	if(CollectionUtils.isNotEmpty(appRolePermissions)){
    		for(AppRolePermission permission : appRolePermissions){
    			permissionIds.add(permission.getPermissionId());
    		}
    	}
    	return permissionIds;
    	/*
        Long[] permissions = getPermissionIdsFromRedis(roleKey);
        if(null == permissions || 0 >= permissions.length) {
            List<AppRolePermission> appRolePermissions = findAppRolePermissionByRoleKey(roleKey);
            permissions = new Long[appRolePermissions.size()];
            for (int i = 0; i < appRolePermissions.size(); i++) {
                AppRolePermission appRolePermission = appRolePermissions.get(i);
                permissions[i] = appRolePermission.getPermissionId();
            }
            putPermissionIds2Redis(roleKey,permissions);
        }
        return permissions;
        */
    }
    
    @Override
    public boolean delAppRolePermissionByRoleKey(String roleKey) {
        appRolePermissionMapper.delAppRolePermissionByRoleKey(roleKey);
        //removeRolePermissionFromRedis(roleKey);
        //removePermissionIdsFromRedis(roleKey);
        return true;
    }
    
    
    public boolean putRolePermission2Redis(String roleKey, List<AppRolePermission> PermissionList) {
        String json  = JSON.toJSONString(PermissionList);
        String group = IcebartechConstants.APP_ROLE_PERMISSION;
        String key   = roleKey;
        redisComponent.putRedisCache(group, key, json);
        return true;
    }
    
    public List<AppRolePermission> getRolePermissionFromRedis(String roleKey) {
        List<AppRolePermission> rtnList = null;
        String                  group   = IcebartechConstants.APP_ROLE_PERMISSION;
        String                  key     = roleKey;
        String                  json    = redisComponent.getRedisCache(group, key);
        if (StringUtils.isNotEmpty(json)) {
            rtnList = JSON.parseArray(json, AppRolePermission.class);
        }
        return rtnList;
    }
    
    public boolean removeRolePermissionFromRedis(String roleKey) {
        String group = IcebartechConstants.APP_ROLE_PERMISSION;
        String key   = roleKey;
        redisComponent.deleteRedisCache(group, key);
        return true;
    }
    
    
    public boolean putPermissionIds2Redis(String roleKey, Long[] PermissionIds) {
        String json  = JSON.toJSONString(PermissionIds);
        String group = IcebartechConstants.APP_PERMISSION_IDS;
        String key   = roleKey;
        redisComponent.putRedisCache(group, key, json);
        return true;
    }
    
    public Long[] getPermissionIdsFromRedis(String roleKey) {
        Long[] permissions = new Long[]{};
        String                  group   = IcebartechConstants.APP_PERMISSION_IDS;
        String                  key     = roleKey;
        String                  json    = redisComponent.getRedisCache(group, key);
        if (StringUtils.isNotEmpty(json)) {
            permissions = JSON.parseObject(json, Long[].class);
        }
        return permissions;
    }
    
    public boolean removePermissionIdsFromRedis(String roleKey) {
        String group = IcebartechConstants.APP_PERMISSION_IDS;
        String key   = roleKey;
        redisComponent.deleteRedisCache(group, key);
        return true;
    }
}
